package GUI;

import java.awt.Dimension;
import java.awt.event.MouseAdapter;

import javax.swing.*;

public class PN_Interval_Panel extends JPanel {

	private static final long serialVersionUID = 2364885747184966760L;
	private PN_Draw_Panel drawPanel;
	JTextField minTime, maxTime;

	public PN_Interval_Panel(PN_Draw_Panel dp){
		this.setBorder(BorderFactory.createTitledBorder("Interval"));
		this.drawPanel = dp;
		
		minTime = new JTextField("");
		maxTime = new JTextField("");
		JTextField label1 = new JTextField("Min");
		JTextField label2 = new JTextField("Max");
		JTextField label3 = new JTextField("millis");
		JTextField label4 = new JTextField("millis");
		JButton button = new JButton("Set Interval Time");
		minTime.setPreferredSize(new Dimension(100,20));
		maxTime.setPreferredSize(new Dimension(100,20));
		minTime.setEditable(true);
		maxTime.setEditable(true);
		minTime.setBorder(BorderFactory.createEtchedBorder());
		maxTime.setBorder(BorderFactory.createEtchedBorder());
		label1.setEditable(false);
		label2.setEditable(false);
		label3.setEditable(false);
		label4.setEditable(false);
		label1.setBorder(BorderFactory.createEmptyBorder());
		label2.setBorder(BorderFactory.createEmptyBorder());
		label3.setBorder(BorderFactory.createEmptyBorder());
		label4.setBorder(BorderFactory.createEmptyBorder());
		this.add(label1);
		this.add(minTime);
		this.add(label3);
		this.add(label2);
		this.add(maxTime);
		this.add(label4);
		this.add(button);
		this.setPreferredSize(new Dimension(180, 130));
		
		button.addMouseListener(new MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				try{
					int a = new Integer(maxTime.getText()),
						b = new Integer(minTime.getText()),
						min = Math.max(1, Math.min(a, b)),
						max = Math.min(Integer.MAX_VALUE, Math.max(a, b));
					System.out.println(min + " " + max);
					drawPanel.SetInterval(min, max);
				} catch(NumberFormatException e) {
					//e.printStackTrace();
				}
			}
		});
	}
}
